package com.tycmc.signelectroniccontracts.act.renzheng;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.tycmc.signelectroniccontracts.R;
import com.tycmc.signelectroniccontracts.act.renzheng.qiye.FraDuiGongDakuan;
import com.tycmc.signelectroniccontracts.act.renzheng.qiye.FraFarenxinxi;
import com.tycmc.signelectroniccontracts.act.renzheng.qiye.FraQIyexinxi;
import com.tycmc.signelectroniccontracts.logic.renzheng.PreQiyeRenZheng;
import com.tycmc.signelectroniccontracts.tools.base.ActBase;
import com.tycmc.signelectroniccontracts.tools.base.FraBase;
import com.tycmc.signelectroniccontracts.tools.other.TextShow;
import com.tycmc.signelectroniccontracts.tools.show.AlertDialog;

import java.util.Map;

public class ActQiyeRenZheng extends ActBase implements PreQiyeRenZheng.QiYeRenZhengInterface {

    private PreQiyeRenZheng pre;
    private FraBase fraQiyexinxi, fraFarenxinxi, fraDuigongdankuan;
    public static final int Qiyexinxi = 1;
    public static final int farenxinxi = 2;
    public static final int renliaanrenzhen = 3;
    public static final int duigongdakuan = 4;
    private boolean isFirstrenlian =true;//是否第一次进行人脸识别

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_qiye_renzheng);
        pre = new PreQiyeRenZheng(this);
        setTitleText("企业认证");
        isFirstrenlian =true;
        initView();

    }

    private void initView() {
        pre.getInfo();
    }

    public void showNum(int num, final Map upMap) {
        hide();
        TextView tvTwo = (TextView) findViewById(R.id.tv_two);
        TextView tvFarenxinxi = (TextView) findViewById(R.id.tv_farenxinx);
        TextView tvoneBack = (TextView) findViewById(R.id.tv_one_back);
        TextView tvThree = (TextView) findViewById(R.id.tv_three);
        TextView tvduigongdaka = (TextView) findViewById(R.id.tv_duigongdakuan);
        TextView tvtwoBack = (TextView) findViewById(R.id.tv_two_back);

        if (num == Qiyexinxi) {
            tvTwo.setTextColor(getResources().getColor(R.color.gray));
            tvFarenxinxi.setTextColor(getResources().getColor(R.color.gray));
            tvThree.setTextColor(getResources().getColor(R.color.gray));
            tvduigongdaka.setTextColor(getResources().getColor(R.color.gray));
            tvoneBack.setBackgroundColor(getResources().getColor(R.color.gray));
            tvtwoBack.setBackgroundColor(getResources().getColor(R.color.gray));
            if (!showHide(fraQiyexinxi)) {
                fraQiyexinxi = new FraQIyexinxi();
                addFra(fraQiyexinxi);
            }
        } else if (num == farenxinxi) {
            tvTwo.setTextColor(getResources().getColor(R.color.colorhead));
            tvFarenxinxi.setTextColor(getResources().getColor(R.color.colorhead));
            tvThree.setTextColor(getResources().getColor(R.color.gray));
            tvduigongdaka.setTextColor(getResources().getColor(R.color.gray));
            tvoneBack.setBackgroundColor(getResources().getColor(R.color.colorhead));
            tvtwoBack.setBackgroundColor(getResources().getColor(R.color.gray));
            if (!showHide(fraFarenxinxi)) {
                fraFarenxinxi = new FraFarenxinxi(upMap);
                addFra(fraFarenxinxi);
            }
        } else if (num == renliaanrenzhen) {
            if(isFirstrenlian){
                pre.renlian(upMap);
                isFirstrenlian =false;
            }else {
                new AlertDialog(ActQiyeRenZheng.this).builder().setTitle("温馨提示").setMsg("认证失败需要重新进行认证").setPositiveButton("认证", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pre.renlian(upMap);
                    }
                }).setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        exit();
                    }
                }).show();
            }

        } else if (num == duigongdakuan) {
            tvTwo.setTextColor(getResources().getColor(R.color.colorhead));
            tvFarenxinxi.setTextColor(getResources().getColor(R.color.colorhead));
            tvThree.setTextColor(getResources().getColor(R.color.colorhead));
            tvduigongdaka.setTextColor(getResources().getColor(R.color.colorhead));
            tvoneBack.setBackgroundColor(getResources().getColor(R.color.colorhead));
            tvtwoBack.setBackgroundColor(getResources().getColor(R.color.colorhead));
            if (!showHide(fraDuigongdankuan)) {
                fraDuigongdankuan = new FraDuiGongDakuan(upMap);
                addFra(fraDuigongdankuan);
            }
        }
    }


    private void addFra(FraBase fraBase) {
        if (fraBase != null) {
            getSupportFragmentManager().beginTransaction().add(R.id.framlayout, fraBase).show(fraBase).commitAllowingStateLoss();
        }
    }

    private void hide() {
        if (fraQiyexinxi != null) {
            getSupportFragmentManager().beginTransaction().hide(fraQiyexinxi).commitAllowingStateLoss();
        }
        if (fraFarenxinxi != null) {
            getSupportFragmentManager().beginTransaction().hide(fraFarenxinxi).commitAllowingStateLoss();
        }
        if (fraDuigongdankuan != null) {
            getSupportFragmentManager().beginTransaction().hide(fraDuigongdankuan).commitAllowingStateLoss();
        }
    }

    private boolean showHide(FraBase fraBase) {
        if (fraBase != null) {
            getSupportFragmentManager().beginTransaction().show(fraBase).commitAllowingStateLoss();
            return true;
        }
        return false;
    }

    @Override
    public void shoujihao(String number) {
        ((TextView) findViewById(R.id.tv_phone_number)).setText("待认证：" + number);
    }

    @Override
    public void initMap(Map map) {
        if (TextShow.isEmpty(TextShow.valueOf(map.get("state")))) {
            showNum(Qiyexinxi, null);
        } else {
            if (TextShow.valueOf(map.get("state")).equals("1")) {//企业信息填写正确
                showNum(farenxinxi, map);
            } else if (TextShow.valueOf(map.get("state")).equals("2")) {//身份信息填写正确
                showNum(renliaanrenzhen, map);
            } else if (TextShow.valueOf(map.get("state")).equals("3")) {//人脸识别正确
                showNum(duigongdakuan, map);
            }else {
                showNum(Qiyexinxi, null);
            }
        }
    }
}
